package org.csu.sei.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.csu.sei.common.BaseConstant;
import org.csu.sei.entity.Banner;
import org.csu.sei.entity.News;
import org.csu.sei.persistence.BannerMapper;
import org.csu.sei.persistence.NewsMapper;
import org.csu.sei.service.IBannerService;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import static org.csu.sei.common.BaseConstant.Status.*;

@Component(value = "bannerService")
public class BannerServiceimpl extends ServiceImpl<BannerMapper, Banner> implements IBannerService {

    @Resource
    private BannerMapper bannerMapper;

    @Resource
    private NewsMapper newsMapper;

    @Override

    public List<Banner> queryAll(boolean selectInvisible){
        QueryWrapper<Banner> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByAsc("banner_order");
        if(selectInvisible){
            return bannerMapper.selectList(queryWrapper);
        }else{
            queryWrapper.eq("banner_status",1);
            return bannerMapper.selectList(queryWrapper);
        }
    }

    @Override
    public Banner queryBanner(Long id,BaseConstant.Status status) {
        if(status==ALL){
            return bannerMapper.selectById(id);
        }
        else{
            QueryWrapper<Banner> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("banner_status",1).eq("id",id);
            return bannerMapper.selectOne(queryWrapper);
        }

    }

    @Override
    public int insertBanner(Banner banner) {
        banner.setCreateTime(LocalDateTime.now());
        banner.setUpdateTime(LocalDateTime.now());
        banner.setBannerStatus(VISIBLE.getCode());
        int result= bannerMapper.insert(banner);
        return result;
    }

    @Override
    public int updateBanner(Banner banner) {
        //判断状态范围
        Banner primary=bannerMapper.selectById(banner.getId());
        if(primary!=null){
            Banner updateBanner = new Banner();
            updateBanner.setId(banner.getId());
            updateBanner.setImage(banner.getImage());
            updateBanner.setKeywords(banner.getKeywords());
            updateBanner.setBannerOrder(banner.getBannerOrder());
            updateBanner.setBannerStatus(banner.getBannerStatus());
            updateBanner.setCreateTime(primary.getCreateTime());
            updateBanner.setUpdateTime(LocalDateTime.now());
            updateBanner.setNewsId(banner.getNewsId());
            return bannerMapper.updateById(updateBanner);
        }
        else{
            return -1;
        }
    }

    @Override
    public int deleteBanner(Long id) {
        if(bannerMapper.selectById(id)==null){
            return -1;
        }
        return bannerMapper.deleteById(id);
    }


    @Override
    public int changeOrder(List<Long> orderList){
        for (int i = 0; i < orderList.size(); i++) {
            Banner banner = bannerMapper.selectById(orderList.get(i));
            if(banner==null)
            {
                return -1;
            }
            banner.setBannerOrder(i+1);
            bannerMapper.updateById(banner);
        }
        return 0;
    }

    @Override
    public int changeStatus(Long id) {
        Banner banner = bannerMapper.selectById(id);
        int res;
        if(banner!=null){
            if(banner.getBannerStatus()== INVISIBLE.getCode()){
                banner.setBannerStatus(VISIBLE.getCode());
                res = VISIBLE.getCode();
            }
            else{
                banner.setBannerStatus(INVISIBLE.getCode());
                res = INVISIBLE.getCode();
            }
            banner.setUpdateTime(LocalDateTime.now());
            bannerMapper.updateById(banner);
            return res;
        }
        else{
            return -1;
        }
    }

    @Override
    public int lastestOrder() {
        return bannerMapper.selectMax();
    }

    @Override
    public int relateNews(Long id,Long newsId){
        //判断是否存在
        News news = newsMapper.selectById(newsId);
        if(news==null){
            return -1;
        }
        //若存在更新banner
        Banner banner=bannerMapper.selectById(id);
        banner.setNewsId(newsId);
        banner.setKeywords(news.getTitle());
        banner.setUpdateTime(LocalDateTime.now());
        bannerMapper.updateById(banner);
        return 0;
    }

    @Override
    public int getCount(){
        return bannerMapper.selectCount(null);
    }
}
